//
//  SCNInvestmentViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/2/6.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNInvestmentViewController: SCNTableViewController {
    
    fileprivate var tenderingDataArray = Array<SCNInvestmentListModels>()
    
    
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        super.viewWillDisappear(animated)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        iOS 11 后导航的大标题属性
//        if #available(iOS 11.0, *) {
//            self.navigationController?.navigationBar.prefersLargeTitles = true
//        } else {
//            // Fallback on earlier versions
//        }
        
        configurationUI()
        
        request()

    
    }

    // MARK: UI
    
    func configurationUI() {
        // 理财指南
        let lcznImgView = UIImageView(image: UIImage(named: "scn_investment_list_head_icon"))
        lcznImgView.isUserInteractionEnabled = true
        view.addSubview(lcznImgView)
        lcznImgView.snp.makeConstraints { (make) in
            make.leading.top.trailing.equalToSuperview()
            make.height.equalTo(338 * SCNConstants.SCREENWIDTH / 720)
        }
        let lczhBtn = UIButton()
        lczhBtn.backgroundColor = .clear
        lczhBtn.addTarget(self, action: #selector(lcznAction), for: .touchUpInside)
        lcznImgView.addSubview(lczhBtn)
        lczhBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 112, height: 35))
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-20)
        }
        // 标列表
        self.createTableView(.zero, style: .plain, isRefreshNewData: true, isRefreshMoreData: false) { (table) in
            
            table.rowHeight = 150
            
            table.separatorStyle = .none
            table.estimatedSectionFooterHeight = 0
            table.estimatedSectionHeaderHeight = 0
            table.register(SCNInvestmentCell.classForCoder(), forCellReuseIdentifier: NSStringFromClass(SCNInvestmentCell.classForCoder()))
        }
    
        self.tableView.snp.makeConstraints { (make) in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(lcznImgView.snp.bottom)
            
        }
        
    }
    func configurationFootView() -> UIView {
        let footView = UIView(frame: CGRect(x: 0, y: 0, width: SCNConstants.SCREENWIDTH, height: 65))
        
        let goBackMoneyBtn = UIButton.init(frame: CGRect.init(x: 10, y: 15, width: SCNConstants.SCREENWIDTH - 20, height: 30))
        goBackMoneyBtn.backgroundColor = SCNConstants.rootBackgroundColor
        goBackMoneyBtn.setTitle("回款中的项目", for: .normal)
        goBackMoneyBtn.addTarget(self, action: #selector(goBackMoneyAction), for: .touchUpInside)
        footView.addSubview(goBackMoneyBtn)
        
        return footView
    }

    // MARK: DATA
    override func request() {
    
        super.request()
        
        let param = [
            "page" : "\(self.pageIndex)",
            "number" : "20",
            "cateid" : "1"
        ]
        
        SCNInvestmentViewModels().requestInvestGetInvestListNew(param, successBlock: { [unowned self] (dataresult) in
            
            if self.pageIndex == 1 {
                self.tenderingDataArray.removeAll()
            }
            
            if let list = dataresult.list {
                for tmp in list {
                    self.tenderingDataArray.append(tmp)
                }
            }
            self.tableView.tableFooterView = self.configurationFootView()
            self.tableView.reloadData()
        })
    }

    // MARK: ACTION
    func goBackMoneyAction() {
        self.scnPushViewController(SCNInvestmentBackMoneyViewController())
    }
    
    func lcznAction() {
        self.scnPushViewController(SCNMoneyGuideViewController())
    }
}

extension SCNInvestmentViewController
{
    

    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return tenderingDataArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(SCNInvestmentCell.classForCoder()), for: indexPath) as! SCNInvestmentCell

        if tenderingDataArray.count > 0 {
            cell.setInvestmentTableViewCell(tenderingDataArray[indexPath.row], index: indexPath, status: 1)
        }
        
        
        return cell
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        defer {
            tableView.deselectRow(at: indexPath, animated: true)
        }
        if tenderingDataArray.count > 0 {
            let detailVC = SCNInvestDetailViewController()
            detailVC.title = tenderingDataArray[indexPath.row].title
            detailVC.loansn = tenderingDataArray[indexPath.row].loansn
            self.scnPushViewController(detailVC)
        }
        
    }
}
